package com.example.demo;

import com.google.common.collect.Maps;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Map;

public class IText7Demo {
    public void createPdf(){
        Map<String, Object> data = Maps.newHashMap();
        InputStream is = null;
        OutputStream os = null;
        try {
            File temp = ResourceUtils.getFile("classpath:pdf/测试Demo.pdf");
            File file = new File("D://测试Demo.pdf");
            is = new FileInputStream(temp);
            os = new FileOutputStream(file);
            generatorPDF(is, os, data);

            System.out.println(temp.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public void generatorPDF(InputStream is, OutputStream os, Map<String, Object> data) throws IOException {
        PdfReader reader = new PdfReader(is);
        PdfWriter writer = new PdfWriter(os);
        PdfDocument pdfDoc = new PdfDocument(reader, writer);
        PdfAcroForm pdfAcroForm = PdfAcroForm.getAcroForm(pdfDoc, true);
        Map<String, PdfFormField> formFields = pdfAcroForm.getFormFields();
        PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H",true);
        PdfFormField agreementId = formFields.get("text1");
        agreementId.setFont(f2);
        agreementId.setValue("wujinwei");
        File temp = ResourceUtils.getFile("classpath:pdf/image.png");
        byte[] buffer = null;
        FileInputStream fis = new FileInputStream(temp);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
        //在指定域位置插入图片
        PdfTextFormField imgF = (PdfTextFormField) formFields.get("text3");
        Rectangle rectangle = imgF.getPdfObject().getAsRectangle(PdfName.Rect);
        float x = rectangle.getLeft();
        float y = rectangle.getBottom();

        Image image = new Image(ImageDataFactory.create(buffer));
        image.setFixedPosition(x,y);
        image.scaleToFit(rectangle.getWidth(), rectangle.getHeight());
        Document doc = new Document(pdfDoc);
        doc.add(image);

        pdfAcroForm.flattenFields();
        doc.close();
        pdfDoc.close();
    }

    public static void main(String[] args) {
        IText7Demo text7 = new IText7Demo();
        text7.createPdf();
    }
}
